package com.yan.demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DeadlockExample {
    private static Lock lock1 = new ReentrantLock();
    private static Lock lock2 = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                lock1.lock();
                System.out.println("Thread 1: Locked lock1");
                Thread.sleep(100);
                lock2.lock();
                System.out.println("Thread 1: Locked lock2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock2.unlock();
                lock1.unlock();
            }
        }).start();

        new Thread(() -> {
            try {
                lock2.lock();
                System.out.println("Thread 2: Locked lock2");
                Thread.sleep(100);
                lock1.lock();
                System.out.println("Thread 2: Locked lock1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock1.unlock();
                lock2.unlock();
            }
        }).start();
    }
}
